package xyz.haijin.shiro.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.haijin.shiro.entity.Role;
import xyz.haijin.shiro.entity.User;
import xyz.haijin.shiro.service.RoleService;
import xyz.haijin.shiro.service.UserService;
import xyz.haijin.shiro.util.MessageUtil;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user.html")
public class UserController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserService userService;

    @RequestMapping(params = "act=page")
    public String page() {
        return "user_list";
    }

    @RequestMapping(params = "act=list")
    @ResponseBody
    public List<User> list() {
        return userService.getUserList();
    }

    @RequestMapping(params = "act=role_list")
    @ResponseBody
    public List<Role> roleList() {
        return roleService.getRoleList();
    }

    @RequestMapping(params = "act=user_role")
    @ResponseBody
    public List<Integer> userRole(Integer userId) {
        return userService.getUserRoleIds(userId);
    }

    @RequestMapping(params = "act=assign_role")
    @ResponseBody
    public Map<String, Object> assignRole(Integer userId, Integer[] roleIds) {
        userService.addUserRole(userId, roleIds);
        return MessageUtil.getSuccessResult("分配成功");
    }
}
